/*
 * Copyright (C) 2010 Fred Barrie
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.Color;

import javax.swing.JTextField;

import org.aergia.vinny.core.ApiResult;
import org.aergia.vinny.database.pojo.VinnyOption;
import org.aergia.vinny.database.pojo.VinnyUser;
import org.aergia.vinny.gui.controller.VinnyController;
import org.aergia.vinny.gui.util.FontUtil;
import org.aergia.vinny.gui.util.LabelPanel;
import org.aergia.vinny.gui.view.EditorDialog.EditorPanel;

public class OptionEditor extends EditorPanel {

	private static final long serialVersionUID = 1L;

	private final VinnyController controller;
	private VinnyOption option;
	private JTextField parameterField;
	private final VinnyUser user;
	private JTextField valueField;

	public OptionEditor(final VinnyController controller, final VinnyUser user) {
		this(controller, user, null);
	}

	/**
	 * @param controller
	 * @param volunteer
	 */
	public OptionEditor(final VinnyController controller, final VinnyUser user,
			final VinnyOption option) {
		this.controller = controller;
		this.user = user;
		this.option = option;
		this.initComponents();
	}

	public VinnyOption getOption() {
		return this.option;
	}

	@Override
	public String getTitle() {
		return "Option Editor";
	}

	@Override
	public boolean saveAction() {
		if (this.option == null) {
			this.option = new VinnyOption();
		}

		if (!this.verify()) {
			return false;
		}

		this.option.setActive(true);
		this.option.setParameter(this.parameterField.getText());
		this.option.setValue(this.valueField.getText());
		this.saveOption();
		return true;
	}

	private void initComponents() {

		this.parameterField = new JTextField(
				this.option != null ? this.option.getParameter() : "");
		this.parameterField.setName("Parameter");
		this.parameterField.setEditable(false);

		this.valueField = new JTextField(
				this.option != null ? this.option.getValue() : "");
		this.valueField.setName("Value");
		this.valueField.setEditable(true);

		this.add(new LabelPanel(FontUtil.getLabelFont(), Color.black,
				this.parameterField, this.valueField));
		this.setVisible(true);
	}

	private void saveOption() {
		final ApiResult<VinnyOption> result = this.controller.saveOption(
				this.option, this.user);
		if (!result.isError()) {
			this.option = result.getResult();
		}
	}

	private boolean verify() {
		return true;

	}
}
